package org.sensmon.core.datamodel;

import java.util.HashMap;
import java.util.Map;

public class GlobalContext {
	
	private Map<Class<?>, Object> globalContextMap;
	
	private GlobalContext() {
		globalContextMap = new HashMap<Class<?>, Object>();
	}
	
	private static class GlobalContextKeeper {
		public static final GlobalContext INSTANCE = new GlobalContext();
	}
	
	public static GlobalContext getInstance() {
		return GlobalContextKeeper.INSTANCE;
	}
	
	public void setValue(Object value) {
		globalContextMap.put(value.getClass(), value);
	}
	
	public Object getValue(Class<?> key) throws Exception {
		if (globalContextMap.get(key) == null) {
			throw new Exception("No such instance in context " + key.getSimpleName());
		}
		return globalContextMap.get(key);
	}
}
